package cc.chinagps.gateway.unit.eg.pkg;

import cc.chinagps.gateway.unit.eg.define.EGDefines;
import org.seg.lib.util.Util;

import java.util.Arrays;

public class EGHead {
    private byte[] startFlag = EGDefines.HEAD_STX;

    private byte cmdId;

    private String vin;

    private int softwareVersion;

    private byte encryption;

    private short bodyLen;

    public byte[] getStartFlag() {
        return startFlag;
    }

    public void setStartFlag(byte[] startFlag) {
        this.startFlag = startFlag;
    }

    public byte getCmdId() {
        return cmdId;
    }

    public void setCmdId(byte cmdId) {
        this.cmdId = cmdId;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    public int getSoftwareVersion() {
        return softwareVersion;
    }

    public void setSoftwareVersion(int softwareVersion) {
        this.softwareVersion = softwareVersion;
    }

    public byte getEncryption() {
        return encryption;
    }

    public void setEncryption(byte encryption) {
        this.encryption = encryption;
    }


    public short getBodyLen() {
        return bodyLen;
    }

    public void setBodyLen(short bodyLen) {
        this.bodyLen = bodyLen;
    }

    public static EGHead parse(byte[] source, int offset) throws Exception {
        EGHead head = new EGHead();
        int position = 0;
        position += offset;
        if (source[0] != EGDefines.HEAD_STX[0] || source[1] != EGDefines.HEAD_STX[1]) {
            throw new Exception("[EG] pkg head stx error");
        }

        byte[] stx = new byte[2];
        System.arraycopy(source, 0, stx, 0, stx.length);
        head.setStartFlag(stx);
        position += 2;

        head.setCmdId(source[position++]);

        head.setVin(new String(source, position, 17, EGDefines.CHARSET));
        position += 17;

        head.setSoftwareVersion(Byte.toUnsignedInt(source[position++]));
        head.setEncryption(source[position++]);

        short bodyLen = Util.getShort(source, position);
        head.setBodyLen(bodyLen);
        position += 2;

        return head;
    }

    public byte[] encode(int dataLen) throws Exception {
        byte[] data = new byte[EGDefines.HEAD_SIZE];
        int pos = 0;
        System.arraycopy(startFlag, 0, data, pos, startFlag.length);
        pos += startFlag.length;

        data[pos++] = cmdId;

        int vinLen = vin.length();
        if (vinLen != 17) {
            throw new Exception("[EG] vin len error,curr len is:" + vinLen + ";vin is:" + vin);
        }

        byte[] vinBytes = vin.getBytes(EGDefines.CHARSET);
        System.arraycopy(vinBytes, 0, data, pos, vinBytes.length);
        pos += vinBytes.length;

        data[pos++] = (byte) softwareVersion;

        data[pos++] = encryption;

        byte[] bodyLenBytes = Util.getShortByte((short) dataLen);
        System.arraycopy(bodyLenBytes, 0, data, pos, bodyLenBytes.length);
        pos += 2;

        return data;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("EGHead [startFlag=");
        builder.append(Arrays.toString(startFlag));
        builder.append(", cmdId=");
        builder.append(cmdId);
        builder.append(", vin=");
        builder.append(vin);
        builder.append(", softwareVersion=");
        builder.append(softwareVersion);
        builder.append(", encryption=");
        builder.append(encryption);
        builder.append(", bodyLen=");
        builder.append(bodyLen);
        builder.append("]");
        return builder.toString();
    }


}
